package com.oak.interviewdemo;

import cn.hutool.core.util.ArrayUtil;

import java.util.Arrays;

/**
 * com.oak.interviewdemo -> EightFunc
 *
 * @Author: guoxy
 * @Description:
 * @Date: 2020/8/31
 */
public class EightFunc {

    public static void main(String[] args) {
        int[] arr = {3, 2, 1, 6, 5, 4, 9, 8, 7};
        System.out.println(Arrays.toString(insertionSort(arr)));
        System.out.println(Arrays.toString(selectionSort(arr)));
    }

    /**
     * 插入排序
     *
     * @param arr
     * @return
     */
    private static int[] insertionSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j >= 0; j--) {
                if (j > 0 && arr[j - 1] > arr[j]) {
                    int temp = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = temp;
                } else {
                    break;
                }
            }
        }
        return arr;
    }

    /**
     * 选择排序
     *
     * @param arr
     * @return
     */
    private static int[] selectionSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }
}